package com.itheima.controller.v1;

import com.itheima.common.R;
import com.itheima.common.RoleResult;
import com.itheima.common.UserResult;
import com.itheima.controller.v1.api.SystemControllerApi;
import com.itheima.pojo.SysRole;
import com.itheima.pojo.SysUser;
import com.itheima.pojo.TreeSelect;
import com.itheima.service.SysDeptService;
import com.itheima.service.SysMenuService;
import com.itheima.service.SysRoleService;
import com.itheima.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/system")
public class SystemController implements SystemControllerApi {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/dept/treeselect")
    public R deptTreeSelect() {
        R r = sysDeptService.treeselect();
        return r;
    }

    @GetMapping("/dept/userlist/{deptId}")
    public R findUserList(@PathVariable Long deptId){
       R r = sysDeptService.findUserList(deptId);
       return r;
    }

    @GetMapping("/user/list")
    public R userList(Integer pageNum, Integer pageSize, SysUser sysUser) {
        R r = sysUserService.findList(pageNum, pageSize, sysUser);
        return r;
    }

    @PutMapping("/user")
    public R addUser(@RequestBody SysUser sysUser) {
        R r = sysUserService.add(sysUser);
        return r;
    }

    @GetMapping("/user")
    public UserResult findRoleAndPost() {
        UserResult result = sysUserService.findRoleAndPost();
        return result;
    }

    @GetMapping("/user/{userId}")
    public UserResult findById(@PathVariable Long userId) {
        UserResult result = sysUserService.findById(userId);
        return result;
    }

    @PostMapping("/user")
    public R modify(@RequestBody SysUser sysUser) {
        R r = sysUserService.modify(sysUser);
        return r;
    }

    @DeleteMapping("/user/{userId}")
    public R delete(@PathVariable Long userId) {
        R r = sysUserService.deleteById(userId);
        return r;
    }

    @PostMapping("/user/status/{number}")
    public R modifyStatus(@PathVariable Integer number, Long userId) {
        R r = sysUserService.modifyStatus(number, userId);
        return r;
    }

    @PostMapping("/user/password/{userId}")
    public R resetPassword(@PathVariable Long userId, String password) {
        R r = sysUserService.ResetPassword(userId, password);
        return r;
    }

    @GetMapping("/role/list")
    public R roleList(Integer pageNum, Integer pageSize, SysRole sysRole) {
        R r = sysRoleService.findList(pageNum, pageSize, sysRole);
        return r;
    }

    @GetMapping("/menu/treeselect")
    public R menuTreeSelect() {
        R r = sysMenuService.menuTreeSelect();
        return r;
    }

    @PutMapping("/role")
    public R addRole(@RequestBody SysRole sysRole){
        R r = sysRoleService.addRole(sysRole);
        return r;
    }

    @GetMapping("/role/{roleId}")
    public RoleResult findRoleById(@PathVariable Long roleId){
        R r = sysMenuService.menuTreeSelect();
        List<TreeSelect> menus =(List<TreeSelect>) r.getData();
        RoleResult result =sysRoleService.findById(roleId);
        result.setMenus(menus);
        return result;
    }

    @PostMapping("/role")
    public R modify(@RequestBody SysRole sysRole){
        R r =sysRoleService.updateRole(sysRole);
        return r;
    }
    @DeleteMapping("/role/{roleId}")
    public R deleteRole(@PathVariable Long roleId){
        R r = sysRoleService.deleteById(roleId);
        return r;
    }
}
